package com.able.concurrency.t1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author abel.he
 * @data 2018/8/15 20:45
 * @describe
 */
public class Test16_3 {
    public static void main(String[] args) {
        final Contina1 contina = new Contina1();
        final CountDownLatch latch = new CountDownLatch(1);
        new Thread(new Runnable() {
            public void run() {
                if (contina.size() != 5) {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("数量已经为5了，可以结束第一个线程了");
                }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("容器现有数量为 " + contina.size());
                    if (contina.size() == 5) {
                        latch.countDown();
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    contina.add(new Object());

                }
            }
        }).start();
    }
}

class Contina1 {
    List<Object> list = new ArrayList<Object>();

    void add(Object o) {
        list.add(o);
    }
    int size() {
        return list.size();
    }
}
